﻿/*function arrayBufferToBase64(arrayBuffer) {
var text = '';
var uint8Array = new Uint8Array(arrayBuffer);
var byteLength = uint8Array.byteLength;
for (var i = 0; i < byteLength; i++) {
text = text + String.fromCharCode(uint8Array[i]);
}
return window.btoa(text);
}
function base64ToArrayBuffer(base64) {
var text = window.atob(base64);
var length = text.length;
var uint8Array = new Uint8Array(length);
for (var i = 0; i < length; i++) {
var charCode = text.charCodeAt(i);
uint8Array.set([charCode], i);
}
return uint8Array.buffer;
}*/
var cacheStore;
var audio;
//var beeps = [];
function testStop() {
	console.log("testStop");
	stopPlayBeeps();
}
function testStart() {
	console.log("testStart");

	var tritonSong = new TritonSong();
	tritonSong.instruments.push(new InstrumentSample("instruments/019/Chaos_000/000-068_-7500_2319-2457.000"));//0
	tritonSong.instruments.push(new InstrumentSample("instruments/000/Chaos_000/053-057_-5500_8-57105.004"));//1
	//tritonSong.instruments.push(new DrumSample("drums/000/Chaos_128/054_-5400.019"));
	tritonSong.instruments.push(new DrumSample("drums/000/Chaos_128/035_-3500.001"));
	tritonSong.instruments.push(new DrumSample("drums/000/Chaos_128/040_-4000.006"));
	tritonSong.instruments.push(new DrumSample("drums/000/Chaos_128/042_-4200.059"));
	
	
	

	var riffChannel1 = new RiffChannel(tritonSong.instruments[1]);
	var riffChannel2 = new RiffChannel(tritonSong.instruments[0]);
	tritonSong.instruments[0].volume=0.5;
	tritonSong.instruments[1].volume=0.1;
	tritonSong.instruments[2].volume=0.1;
	tritonSong.instruments[3].volume=0.1;
	tritonSong.instruments[4].volume=0.1;
	var b=48;
	
	riffChannel1.notes.push(new ChannelNote(0, b, 4, 0));
	riffChannel1.notes.push(new ChannelNote(4, b - 5, 4, 0));
	riffChannel1.notes.push(new ChannelNote(8, b, 4, 0));
	riffChannel1.notes.push(new ChannelNote(12, b - 5, 4, 0));
	riffChannel1.notes.push(new ChannelNote(16, b, 4, 0));
	riffChannel1.notes.push(new ChannelNote(20, b - 5, 4, 0));
	riffChannel1.notes.push(new ChannelNote(24, b, 2, 0));
	riffChannel1.notes.push(new ChannelNote(26, b - 5, 2, 0));
	riffChannel1.notes.push(new ChannelNote(28, b - 3, 2, 0));
	riffChannel1.notes.push(new ChannelNote(30, b - 2, 2, 0));
	
	//
	riffChannel2.notes.push(new ChannelNote(0, b+12+4, 24, 0));
	//
	
	riffChannel2.kicks.push(new ChannelKick(0, tritonSong.instruments[2]));
	riffChannel2.kicks.push(new ChannelKick(4, tritonSong.instruments[3]));
	riffChannel2.kicks.push(new ChannelKick(8, tritonSong.instruments[2]));
	riffChannel2.kicks.push(new ChannelKick(12, tritonSong.instruments[3]));
	riffChannel2.kicks.push(new ChannelKick(16, tritonSong.instruments[2]));
	riffChannel2.kicks.push(new ChannelKick(20, tritonSong.instruments[3]));
	riffChannel2.kicks.push(new ChannelKick(24, tritonSong.instruments[2]));
	riffChannel2.kicks.push(new ChannelKick(28, tritonSong.instruments[3]));
	
	riffChannel2.kicks.push(new ChannelKick(0, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(2, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(4, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(6, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(8, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(10, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(12, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(14, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(16, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(18, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(20, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(22, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(24, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(26, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(28, tritonSong.instruments[4]));
	riffChannel2.kicks.push(new ChannelKick(30, tritonSong.instruments[4]));
	
	

	var songSlot1 = new SongSlot();
	songSlot1.step = 0;
	songSlot1.channels.push(riffChannel1);
	songSlot1.channels.push(riffChannel2);
	
	
	
	tritonSong.slots.push(songSlot1);

	cacheStore.loadInstruments(tritonSong.instruments //
	, function () {
		console.log("afterError");
	} //
	, function (instrumentSample) {
		console.log("beforeProgress ", instrumentSample);
	} //
	, function () {
		console.log("afterSuccess");
		//var resampled = audio.resample(instrumentSamples[0].float32Array, -7500, 0.95, 120, 8, 60, 2319, 2457, 60);
		//var resampled1=audio.resample(instrumentSamples[1].float32Array,-5500,0.95,120,8,60+4,8,57105,60+4);
		//var resampled1 = audio.resample(instrumentSamples[0].float32Array, -7500, 0.95, 120, 8, 60 + 4, 2319, 2457, 60 + 4);
		//var beep = null;
		//beeps = [];


		triBeeps.splice(0, triBeeps.length)
		/*
		triBeeps.push(audio.renderBeep(0, tritonSong.instruments[1], 120, 4, 60, 60));
		triBeeps.push(audio.renderBeep(4, tritonSong.instruments[1], 120, 4, 60 - 12, 60 - 12));
		triBeeps.push(audio.renderBeep(8, tritonSong.instruments[1], 120, 4, 60, 60));
		triBeeps.push(audio.renderBeep(12, tritonSong.instruments[1], 120, 4, 60 - 12, 60 - 12));
		 */
		/*
		audio.renderBeep(0, tritonSong.instruments[1], 120, 4, 60, 60);
		audio.renderBeep(4, tritonSong.instruments[1], 120, 4, 60 - 12, 60 - 12);
		audio.renderBeep(8, tritonSong.instruments[1], 120, 4, 60, 60);
		audio.renderBeep(12, tritonSong.instruments[1], 120, 4, 60 - 12, 60 - 12);
		 */
		//triBeeps[0].beep();
		//triBeeps[1].beep();
		startPlayBeeps(audio, 120, 32, tritonSong, function (n) {
			//console.log("		progress",n);
		});
		/*beeps.push(beep);
		var b1 = audio.renderBeep(instrumentSamples[0], 0.95, 120, 4, 60 + 4, 60 + 4);

		b.beep();
		b1.beep();
		 */

	} //
	);

}
function test2(audio, instrumentSamples) {
	var resampled = audio.resample(instrumentSamples[0].float32Array, -7500, 0.95, 120, 8, 60, 2319, 2457, 60);
	//var resampled1=audio.resample(instrumentSamples[1].float32Array,-5500,0.95,120,8,60+4,8,57105,60+4);
	var resampled1 = audio.resample(instrumentSamples[0].float32Array, -7500, 0.95, 120, 8, 60 + 4, 2319, 2457, 60 + 4);
	var b = audio.renderBeep(resampled);
	var b1 = audio.renderBeep(resampled1);
	b.beep();
	b1.beep();
}
function a2() {
	console.log("start a2");

	cacheStore = new CacheStore();
	audio = new Audio5();
	audio.initAudioContext();
	var key = "test12";
	/*
	cacheStore.drop(key);
	var is=new InstrumentSample();
	is.parse("http://www.javafx.me/extracted16/instruments/019/Chaos_000/000-068_-7500_2319-2457.000");
	console.log(is);
	 */
	/*
	var instrumentSamples = new Array(2);
	instrumentSamples[0] = new InstrumentSample("instruments/019/Chaos_000/000-068_-7500_2319-2457.000");
	instrumentSamples[1] = new InstrumentSample("instruments/000/Chaos_000/053-057_-5500_8-57105.004");
	cacheStore.loadInstruments(instrumentSamples //
, function () {
	console.log("afterError");
	} //
, function (instrumentSample) {
	console.log("beforeProgress ",instrumentSample);
	} //
, function () {
	console.log("afterSuccess");
	test2(audio,instrumentSamples);
	} //
	);
	 */
	/*
	cacheStore.fetchSample(//
	//"http://www.javafx.me/extracted16/instruments/000/Chaos_000/053-057_-5500_8-57105.004" //
	//"http://www.javafx.me/extracted16/instruments/019/Chaos_000/069-096_60_-8700_2377-2446.001.16"//
	//"http://www.javafx.me/extracted16/instruments/019/Chaos_000/000-068_60_-7500_2319-2457.000.16"//
	"http://www.javafx.me/extracted16/instruments/019/Chaos_000/000-068_-7500_2319-2457.000"//,key //, function (float32Array) {
	console.log("ok");
	//sampleFloat32Array, frequencyShift, volume, tempo, length, targetFrequency, loopStart, loopEnd, slideFrequency) {
	//var pitch=60;
	//var correction=60;
	//var startFrequency = pitch + 60 + correction / 100.0;
	var resampled=audio.resample(float32Array,-7500,0.95,120,8,60,2319,2457,63);
	audio.playSample(resampled);
	}, function () {
	console.log("ops");
	});
	 */
	/*
	var mm=cacheStore.findInMemory("tst123");
	console.log(mm);
	var mm=cacheStore.findInCache("tst123");
	console.log(mm);*/
	/*
	httpGETArray("http://www.javafx.me/extracted16/instruments/000/Chaos_000/093-127_60_-9700_8-8089.010.16", function (arrayBuffer) {
	var uint8Array = new Uint8Array(arrayBuffer);
	for(var i=0;i<9;i++){
	console.log(i,uint8Array[i]);
	}
	var b64=arrayBufferToBase64(arrayBuffer);
	var a=base64ToArrayBuffer(b64);
	//console.log("afterSuccess", arrayBufferToBase64(arrayBuffer));
	var uint8Array = new Uint8Array(a);
	for(var i=0;i<9;i++){
	console.log(i,uint8Array[i]);
	}
	}, function (xmlHttpRequest) {
	console.log("afterStatus", xmlHttpRequest);
	}, function (xmlHttpRequestProgressEvent) {
	console.log("afterError", xmlHttpRequestProgressEvent);
	});*/
	/*var arrayBuffer = new ArrayBuffer(5);
	arrayBuffer[0] = 0;
	arrayBuffer[1] = 1;
	arrayBuffer[2] = 222;
	arrayBuffer[3] = 33;
	arrayBuffer[4] = 44;
	var b64 = arrayBufferToBase64(arrayBuffer);
	console.log(b64);
	var a = base64ToArrayBuffer(b64);
	console.log("a.byteLength",a.byteLength);
	for (var i = 0; i < a.byteLength; i++) {
	console.log(i,a[i]);
	}*/
}
